#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
---------------------------------------------------

@file: SmsSend.py

@author: 'Ovan'

@mtime: '2018/9/6'

@desc:


    `````````````````````````````````


---------------------------------------------------
"""
import json
import logging

import requests

log_sms = logging.getLogger('mail')


class SendSms(object):
    """
    短信 发送 业务
    """

    def __init__(self):
        self.account = 'N656426_N6402722'
        self.password = 'No72GPsMwY06b3'
        self.single_send_url = "https://smssh1.253.com/msg/send/json"

    def send_sms(self, code, phone):
        parmas = {
            "account": self.account,
            'password': self.password,
            "phone": phone,
            "msg": "【医鸽】请保证您已阅读使用条款和隐私政策，验证码：{code}。".format(code=code)  # 改
        }

        response = requests.post(self.single_send_url, json=parmas)
        re_dict = json.loads(response.text)
        log_sms.info('发送短信 parmas=%s, TO: %s CODE: %s。 响应数据: %s .' % (parmas, phone, code, re_dict))
        return re_dict


if __name__ == '__main__':
    send = SendSms()
    get = send.send_sms(code='1234', phone='15001963096')
    print('context :', get)
    print(type(get))

# def ScrfCode(req):
#     tele = req.POST['phone']
#     if not UserTB.objects.filter(phone=tele):
#         logger = logging.getLogger("tasks")
#         n = functools.reduce(lambda x, y: 10 * x + y, [random.randint(1, 9) for x in range(6)])
#         http = "http://smssh1.253.com/msg/send/json"
#         statement = requests.post(http, json={"account": "N6402722", "password": "KkoPMCzYpE83aa", "phone": "%s" % tele,
#                                               "msg": "【安迪生物科技】您的验证码是:%s,如非本人操作，请忽略此消息"%n})
#         req.session["code"] = n
#         logger.info('发送验证码:%s:%s' % (tele, statement.content))
#         return JsonResponse({'ret': 0,'msg':'success'})
#     return JsonResponse({'ret':1,'msg':'该手机号已被注册'})
